/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERNP_ENTITY_MESSAGES_HPP
#define XERNP_ENTITY_MESSAGES_HPP

#include "Xern/Hashes.hpp"
#include "Xern/Vector.hpp"
#include "Xern/Quaternion.hpp"
#include "Message.hpp"

namespace XernProtocol
{
	class EntityMovementUpdates: public Message
	{
	public:
		static const int MaxUpdatesPerPacket = 10;

		struct Update
		{
			static const size_t UpdateSize = 92;
			uuid id;
			uuid parentId;
			Vector3 position;
			Vector3 velocity;
			Vector3 acceleration;
			Quaternion rotation;
			Vector3 angularVelocity;
		};

		EntityMovementUpdates();
		~EntityMovementUpdates();

		virtual MessageType GetType() const;
		virtual size_t GetSize();

		virtual void Decode(DataStream &in, size_t size);
		virtual void Encode(DataStream &out) const;

		u64 time;
		// u8 numupdates;
		mutable std::vector<Update> updates;
	};

	class EntityPhysicUpdates: public Message
	{
	public:
		static const int MaxUpdatesPerPacket = 10;

		struct Update
		{
			static const size_t UpdateSize = 69;
			u8 physicType; // IRigidBody::RigidBodyType
			float mass;
			uuid entity;
			uuid collisionShape;
			Vector3 linearFactor;
			Vector3 angularFactor;
			float linearSleepThreshold;
			float angularSleepThreshold;
		};

		EntityPhysicUpdates();
		~EntityPhysicUpdates();

		virtual MessageType GetType() const;
		virtual size_t GetSize();

		virtual void Decode(DataStream &in, size_t size);
		virtual void Encode(DataStream &out) const;

		u64 time;
		// u8 numupdates;
		mutable std::vector<Update> updates;
	};

	class EntityAdded: public Message
	{
	public:
		static const int MaxEntitiesPerPacket = 10;

		struct Data
		{
			static const size_t DataSize = 32;
			uuid parentId;
			uuid id;
		};

		EntityAdded();
		~EntityAdded();

		virtual MessageType GetType() const;
		virtual size_t GetSize();

		virtual void Decode(DataStream &in, size_t size);
		virtual void Encode(DataStream &out) const;

		u64 time;
		// u8 numentities;
		std::vector<Data> entityData;
	};

	class EntityRemoved: public Message
	{
	public:
		static const int MaxEntitiesPerPacket = 10;

		EntityRemoved();
		~EntityRemoved();

		virtual MessageType GetType() const;
		virtual size_t GetSize();

		virtual void Decode(DataStream &in, size_t size);
		virtual void Encode(DataStream &out) const;

		u64 time;
		// u8 numentities;
		std::vector<uuid> entities;
	};

	class EntityAddedResource: public Message
	{
	public:
		static const size_t Size = 74;

		EntityAddedResource();
		~EntityAddedResource();

		virtual MessageType GetType() const;
		virtual size_t GetSize();

		virtual void Decode(DataStream &in, size_t size);
		virtual void Encode(DataStream &out) const;

		uuid entityId;
		uuid resourceId;
		Sha256 resourceHash;
		u32 resourceFlags;
		// u16 mimesize;
		std::string resourceMime;
		// u16 extensionSize;
		std::string resourceExtension;
	};

	class EntityRemovedResource: public Message
	{
	public:
		static const size_t Size = 32;

		EntityRemovedResource();
		~EntityRemovedResource();

		virtual MessageType GetType() const;
		virtual size_t GetSize();

		virtual void Decode(DataStream &in, size_t size);
		virtual void Encode(DataStream &out) const;

		uuid entityId;
		uuid resourceId;

	};
}; // namespace XernProtocol

#endif //XERNP_ENTITY_MESSAGES_HPP
